<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="读者编号" prop="readerNo">
        <el-input
          v-model="queryParams.readerNo"
          placeholder="请输入读者编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="读者姓名" prop="readerName">
        <el-input
          v-model="queryParams.readerName"
          placeholder="请输入读者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bookSys:reader:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bookSys:reader:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bookSys:reader:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bookSys:reader:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="readerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="读者ID" align="center" prop="readerId" />
      <el-table-column label="读者编号" align="center" prop="readerNo" />
      <el-table-column label="读者姓名" align="center" prop="readerName" />
      <el-table-column label="联系电话" align="center" prop="readerPhone" />
      <el-table-column label="电子邮箱" align="center" prop="readerEmail" />
      <el-table-column label="读者类型" align="center" prop="readerType" />
      <el-table-column label="注册日期" align="center" prop="registerDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="读者证过期日期" align="center" prop="expireDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最大可借数量" align="center" prop="maxBorrowNum" />
      <el-table-column label="当前已借数量" align="center" prop="currentBorrowNum" />
      <el-table-column label="读者状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bookSys:reader:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bookSys:reader:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改读者信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="读者编号" prop="readerNo">
          <el-input v-model="form.readerNo" placeholder="请输入读者编号" />
        </el-form-item>
        <el-form-item label="读者姓名" prop="readerName">
          <el-input v-model="form.readerName" placeholder="请输入读者姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="readerPhone">
          <el-input v-model="form.readerPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="readerEmail">
          <el-input v-model="form.readerEmail" placeholder="请输入电子邮箱" />
        </el-form-item>
        <el-form-item label="注册日期" prop="registerDate">
          <el-date-picker clearable
            v-model="form.registerDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择注册日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="读者证过期日期" prop="expireDate">
          <el-date-picker clearable
            v-model="form.expireDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择读者证过期日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最大可借数量" prop="maxBorrowNum">
          <el-input v-model="form.maxBorrowNum" placeholder="请输入最大可借数量" />
        </el-form-item>
        <el-form-item label="当前已借数量" prop="currentBorrowNum">
          <el-input v-model="form.currentBorrowNum" placeholder="请输入当前已借数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-divider content-position="center">图书借阅记录信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBookBorrow">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBookBorrow">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="bookBorrowList" :row-class-name="rowBookBorrowIndex" @selection-change="handleBookBorrowSelectionChange" ref="bookBorrow">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="图书ID" prop="bookId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.bookId" placeholder="请输入图书ID" />
            </template>
          </el-table-column>
          <el-table-column label="借阅单号" prop="borrowNo" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.borrowNo" placeholder="请输入借阅单号" />
            </template>
          </el-table-column>
          <el-table-column label="借出日期" prop="borrowDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.borrowDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择借出日期" />
            </template>
          </el-table-column>
          <el-table-column label="计划归还日期" prop="planReturnDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.planReturnDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择计划归还日期" />
            </template>
          </el-table-column>
          <el-table-column label="实际归还日期" prop="actualReturnDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.actualReturnDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择实际归还日期" />
            </template>
          </el-table-column>
          <el-table-column label="逾期天数" prop="overdueDays" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.overdueDays" placeholder="请输入逾期天数" />
            </template>
          </el-table-column>
          <el-table-column label="逾期费用" prop="overdueFee" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.overdueFee" placeholder="请输入逾期费用" />
            </template>
          </el-table-column>
          <el-table-column label="费用状态" prop="feeStatus" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.feeStatus" placeholder="请选择费用状态">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="借阅状态" prop="borrowStatus" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.borrowStatus" placeholder="请选择借阅状态">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="操作人" prop="operator" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.operator" placeholder="请输入操作人" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReader, getReader, delReader, addReader, updateReader } from "@/api/bookSys/reader"

export default {
  name: "Reader",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBookBorrow: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 读者信息表格数据
      readerList: [],
      // 图书借阅记录表格数据
      bookBorrowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        readerNo: null,
        readerName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        readerNo: [
          { required: true, message: "读者编号不能为空", trigger: "blur" }
        ],
        readerName: [
          { required: true, message: "读者姓名不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询读者信息列表 */
    getList() {
      this.loading = true
      listReader(this.queryParams).then(response => {
        this.readerList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        readerId: null,
        readerNo: null,
        readerName: null,
        readerPhone: null,
        readerEmail: null,
        readerType: null,
        registerDate: null,
        expireDate: null,
        maxBorrowNum: null,
        currentBorrowNum: null,
        status: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      }
      this.bookBorrowList = []
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.readerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加读者信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const readerId = row.readerId || this.ids
      getReader(readerId).then(response => {
        this.form = response.data
        this.bookBorrowList = response.data.bookBorrowList
        this.open = true
        this.title = "修改读者信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.bookBorrowList = this.bookBorrowList
          if (this.form.readerId != null) {
            updateReader(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addReader(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const readerIds = row.readerId || this.ids
      this.$modal.confirm('是否确认删除读者信息编号为"' + readerIds + '"的数据项？').then(function() {
        return delReader(readerIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
	/** 图书借阅记录序号 */
    rowBookBorrowIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 图书借阅记录添加按钮操作 */
    handleAddBookBorrow() {
      let obj = {}
      obj.bookId = ""
      obj.borrowNo = ""
      obj.borrowDate = ""
      obj.planReturnDate = ""
      obj.actualReturnDate = ""
      obj.overdueDays = ""
      obj.overdueFee = ""
      obj.feeStatus = ""
      obj.borrowStatus = ""
      obj.operator = ""
      obj.remark = ""
      this.bookBorrowList.push(obj)
    },
    /** 图书借阅记录删除按钮操作 */
    handleDeleteBookBorrow() {
      if (this.checkedBookBorrow.length == 0) {
        this.$modal.msgError("请先选择要删除的图书借阅记录数据")
      } else {
        const bookBorrowList = this.bookBorrowList
        const checkedBookBorrow = this.checkedBookBorrow
        this.bookBorrowList = bookBorrowList.filter(function(item) {
          return checkedBookBorrow.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleBookBorrowSelectionChange(selection) {
      this.checkedBookBorrow = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bookSys/reader/export', {
        ...this.queryParams
      }, `reader_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
